package de.au.fi.imp.reader.yahoodata;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.au.fi.calc.CalcHelper;
import de.au.fi.domain.EntityHelper;
import de.au.fi.domain.Stock;
import de.au.fi.integration.StockHelperTest;

public class YahooResultMapper {

	Logger log = LoggerFactory.getLogger(this.getClass());

	public List<Stock> mapYahooDataToStocks(List<String> yahooResult) {
		List<Stock> stocks = new ArrayList<Stock>();
		for (String stockInfo : yahooResult)
		{
			Stock stock =mapYahooDataToStock(stockInfo);
			if(stock != null)
				stocks.add(stock);
		}
		log.debug("mapped {} stocks", stocks.size());
		return stocks;
	}

	public Stock mapYahooDataToStock(String stockInfo) {
		List<String> values = readDataStringToList(stockInfo);

		Stock stock = new Stock();
		String value;
		for (int i = 0; i < values.size(); i++) {
			value = values.get(i);
			switch (i) {
			case 0:
				stock.setTicker(value);
				break;
			case 1:
				stock.setName(value);
				break;
			case 2:
				stock.setKursNow(CalcHelper.getDoubleSafe(value));
				break;
			case 3:
				stock.setMarktKapInMio(getMarketCap(value));
				stock.setCapTypeByMarktKap();
				break;
			case 4:
				stock.setKgvAj(CalcHelper.getDoubleSafe(value));
				break;
			case 5:
				// FIXME
				stock.setEbitLj(null);
				break;
			case 6:
				stock.setEpsAj(CalcHelper.getDoubleSafe(value));
				break;
			case 7:
				stock.setEpsNj(CalcHelper.getDoubleSafe(value));
				break;

			default:
				break;
			}
		}
		if(stock.getTicker().equals(stock.getName()) && stockInfo.contains("N/A"))
		{
			log.error("No data for ticker {} data returned {}", stock.getTicker(), stockInfo);
			return null;
		}
			
		addHistoricKurse(stock);
		log.debug(EntityHelper.log(stock));
		return stock;
	}

	private void addHistoricKurse(Stock stock) {
		try {
			List<Double> historicKurse = new YahooDataReader()
					.getHistoricKurse(stock.getTicker());
			stock.setKurs6monate(historicKurse.get(0));
			stock.setKurs1jahr(historicKurse.get(1));
		} catch (Exception e) {
			log.error("Could not get Historic kurse for {} {}",
					stock.getTicker(), stock.getName());
		}
	}

	private List<String> readDataStringToList(String stockInfo) {
		List<String> values = new ArrayList<String>();
		StringTokenizer tokenizer = new StringTokenizer(stockInfo, "\"", false);
		int count = 1;
		while (tokenizer.hasMoreElements()) {
			String string = (String) tokenizer.nextElement();
			string.replace("\"", "");
			if (count == 1)
				values.add(string);
			if (count == 2)
				;
			if (count == 3)
				values.add(string);
			if (count == 4) {
				stockInfo = string;
				break;
			}
			count++;
		}
		tokenizer = new StringTokenizer(stockInfo, ",", false);
		while (tokenizer.hasMoreElements())
			values.add((String) tokenizer.nextElement());
		return values;
	}

	private Double getMarketCap(String value) {
		int factor = 1;
		if (value.endsWith("B"))
			factor = 1000000000;
		if (value.endsWith("M"))
			factor = 1000000;
		Double val;
		try {
			val = new Double(value.replace("M", "").replace("B", ""));
		} catch (NumberFormatException e) {
			log.error("Could not get market cap:{}",e.getMessage());
			return null;
		}
		return factor * val / 1000000;
	}
}
